//加载字典
function dictJudge(value, options, row) {
    //sex为自定义字典项
    var statusObj = vm.options.sex;
    for (var i = 0; i < statusObj.length; i++) {
        if (statusObj[i].value == value) {
            return '<span>' + statusObj[i].text + '</span>';
        }
    }
}
$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'cgs/tcgsbusinessbookinfo/list',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', index: 'id', width: 50, key: true, hidden: true},
            {label: '预约业务id', name: 'businessId', index: 'business_id', width: 80, hidden: true},
            {label: '业务名称', name :"businessName", index: 'business_name' ,width:120},
            {label: '用户标识', name: 'userId', index: 'user_id', width: 80},
            {label: '车管所id', name: 'cgsId', index: 'cgs_id', width: 80, hidden: true},
            {label: '车管所', name: 'cgsName', index: 'cgs_name', width: 120},
            {label: '预约日期', name: 'bookDate', index: 'book_date', width: 80},
            {label: '预约时间', name: 'bookTime', index: 'book_time', width: 80},
            {
                label: '预约状态',
                name: 'status',
                index: 'status',
                width: 80,
                formatter: function (value, option, row) {
                    if (value == '0') {
                        return "<span>已预约</span>";
                    } else if (value == '1') {
                        return "<span>已到达</span>";
                    } else if (value == "2") {
                        return "<span>已取消</span>";
                    }
                }
            },
            {label: '到达时间', name: 'arriveTime', index: 'arrive_time', width: 80},
            {label: '取消时间', name: 'cancelTime', index: 'cancel_time', width: 80},
            {label: '创建时间', name: 'createTime', index: 'create_time', width: 80}
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });

    //初始化日期范围组件
    $('#datepicker_range input').datepicker({
        language: 'zh-CN',//显示中文
        startView: 0, //选择器开始界面 0：天（默认 1：月2：年3：十年 4：世纪
        minViewMode: 0,//最小选择精度 0：天（最小默认）
        maxViewMode: 2, //最大选择精度 4：世纪（最大默认）
        format: 'yyyy-mm-dd',//显示格式
        minView: "day",//设置只显示到月份
        // initialDate: new Date(),
        autoclose: true,//选中自动关闭
        // todayBtn: true//显示今日按钮
        clearBtn: true //清除按钮
        //todayHighlight: true //今日高亮
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        tCgsBusinessBookInfo: {},
        cgsList:[]
    },
    mounted:function(){
        this.$nextTick(function () {
            this.getCgsList()
        })
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.tCgsBusinessBookInfo = {};
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            var url = vm.tCgsBusinessBookInfo.id == null ? "cgs/tcgsbusinessbookinfo/save" : "cgs/tcgsbusinessbookinfo/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.tCgsBusinessBookInfo),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "cgs/tcgsbusinessbookinfo/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            $.get(baseURL + "cgs/tcgsbusinessbookinfo/info/" + id, function (r) {
                vm.tCgsBusinessBookInfo = r.tCgsBusinessBookInfo;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            var postData = $("#jqGrid").jqGrid('getGridParam', 'postData');
            $.each(postData, (k, v) => {
                delete postData[k];
            });
            var params = {
                "businessName":$.isEmptyObject($("#businessName").val())?undefined:$("#businessName").val(),
                "userId":$.isEmptyObject($("#userId").val())?undefined:$("#userId").val(),
                "startDate":$.isEmptyObject($("#datepicker_start").val())?undefined:$("#datepicker_start").val(),
                "endDate":$.isEmptyObject($("#datepicker_end").val())?undefined:$("#datepicker_end").val(),
                "bookStatus":$.isEmptyObject($("#bookState").val())?undefined:$("#bookState").val(),
                "cgsId":$.isEmptyObject($("#cgsId").val())?undefined:$("#cgsId").val()
            };
            $("#jqGrid").jqGrid('setGridParam', {
                page: page,
                postData:params
            }).trigger("reloadGrid");
        },
        getCgsList:function(){
            $.ajax({
                url:baseURL+"cgs/tcgsbusinessbookinfo/queryCgsForList",
                type:"POST",
                dataType:"json",
                success:function(data,status){
                    if(data.code == 0){
                        vm.cgsList = data.list;
                    }
                },
                error:function(xmlhttprequest,errormessage,throwException){
                    alert(errormessage);
                }
            });
        }
    }
});